﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryHW
{
    //CityPhoneBook object will be crated for each city
    class CityPhoneBook
    {
        public CityPhoneBook()
        { }

        public Dictionary<int, string> LocalPhoneBook = new Dictionary<int, string>();
    }
    //Main Class that will contain all the towns with their phonebooks
    class General
    {
        //main Collection that contains City name and Cyty PhoneBook
        private Dictionary<string, Dictionary<int, string>> CityList = new Dictionary<string, Dictionary<int, string>>();
        //Default Constructor
        public General() { }
        //Start user menu implementation
        public void StartMenu()
        {
            //Stupid Start Menu
            Console.WriteLine("Dictionary Main Menu");
            Console.WriteLine("1 - Add City");
            Console.WriteLine("2 - Add Number");
            Console.WriteLine("3 - Number Search");
            Console.WriteLine("4 - Show city phonebook");
            Console.WriteLine("5 - Exit");
            Console.WriteLine("6 - To clear the screen");
            //Switch on user input
            switch (Console.ReadLine())
            {
                case "1":
                    AddCity();
                    break;
                case "2":
                    AddNumber();
                    break;
                case "3":
                    SearchByNumber();
                    break;
                case "4":
                    ShowCityPhoneBook();
                    break;
                case "5":
                    Console.WriteLine("Thank you for using our PhoneBook");
                    System.Threading.Thread.Sleep(3000);
                    break;
                case "6":
                    Console.Clear();
                    StartMenu();
                    break;
                default:
                    Console.WriteLine("Wrong Input");
                    System.Threading.Thread.Sleep(1200);
                    Console.Clear();
                    StartMenu();
                    break;
            }
        }
        //Adding City
        private void AddCity()
        {
            Console.WriteLine("Please input the new City Name");
            string city = Utils.GetOnlyLetters();

            if ((Utils.IsOnlyLetters(city)) && (isExist(city)))
            {
                CityList.Add(city, new CityPhoneBook().LocalPhoneBook);
                Console.WriteLine("{0} Added to City List", city);
                System.Threading.Thread.Sleep(1000);
                Console.Clear();
            }
            else
            {
                Console.WriteLine("Wrong Input");
                AddCity();
            }

            StartMenu();
        }
        //Add Number
        private void AddNumber()
        {
            //Printing City List
            Console.Clear();
            Console.WriteLine("Available City List");
            Console.WriteLine();
            if (CityList.Count > 0)
            {
                foreach (var item in CityList)
                {
                    Console.WriteLine(item.Key);
                }
                Console.WriteLine("Input City name to add the Number:  ");
                Console.WriteLine();
                string city = Console.ReadLine();
                //Looping in CityList Dictionary to find record with city we need
                foreach (var item in CityList)
                {
                    if (item.Key == city)
                    {
                        Console.WriteLine("Please input phone number");
                        int phone = Utils.GetNumber();
                        Console.WriteLine("Please input person name");
                        string name = Utils.GetOnlyLetters();

                        item.Value.Add(phone, name);
                    }
                }
            }
            else
            {
                Console.WriteLine("City List is Empty Please add City First");
                System.Threading.Thread.Sleep(2300);
                Console.Clear();
            }
            StartMenu();
        }
        //Search city
        private void SearchByNumber()
        {
            Console.WriteLine("Please input the phone number");
            int i = Convert.ToInt32(Console.ReadLine());
            if (CityList.Count > 0)
            {
                foreach (var item in CityList)
                {
                    foreach (var number in item.Value)
                    {
                        if (number.Key == i)
                        {
                            Console.WriteLine("User Found");
                            Console.WriteLine("{0} {1}", number.Key, number.Value);
                            System.Threading.Thread.Sleep(3500);
                            Console.Clear();
                        }
                        else
                        {
                            Console.WriteLine("No Item Found");
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("No Items in Dictionary");
                System.Threading.Thread.Sleep(1000);
                Console.Clear();
            }
            StartMenu();
        }
        //Shows Selected City PhoneBook
        private void ShowCityPhoneBook()
        {
            
            Console.WriteLine("City List: ");
            //Printing CityList
            foreach (var item in CityList)
            {
                Console.WriteLine("{0}", item.Key);
            }
            //Weak Point need to add check for input
            Console.WriteLine("Please choose City to view the PhoneBook or Exit to exit main menu");
            string city = Utils.GetOnlyLetters();
            //Getting in city Dictionary to print all contaning numbers
            if ((CityList.Keys.Contains(city)) && (CityList.Count > 0))
            {
                foreach (var item in CityList)
                {
                    if (item.Key == city)
                    {
                        var getCollect = item.Value.Keys;
                        Console.WriteLine("{0} Contains {1} Phone number/numbers", item.Key, item.Value.Count);
                        foreach (int i in getCollect)
                        {
                            Console.WriteLine("- {0}", i.ToString());
                        }
                        StartMenu();
                    }
                }
            }
            else
            {
                if (city == "exit")
                {
                    StartMenu();
                }
            }
            ShowCityPhoneBook();
        }
        //check if city is already in list
        private bool isExist(string city)
        {
            if (CityList.Keys.Contains(city))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
